package jzoffer;

import java.util.PriorityQueue;

/**
 * @author MaoLin Wang
 * @date 2020/10/19 11:16 上午
 */
public class _41数据流中的中位数 {

    private PriorityQueue<Integer>small;
    private PriorityQueue<Integer>big;



    public _41数据流中的中位数() {
        small=new PriorityQueue<>();
        big=new PriorityQueue<>((x,y)->(y-x));

    }

    public void addNum(int num) {
        if (small.size()!=big.size()){
            small.add(num);
            big.add(small.poll());
        }else {
            big.add(num);
            small.add(big.poll());
        }

    }

    public double findMedian() {
        return small.size()==big.size()? (small.peek()+big.peek())/2.00: small.peek();
    }
}
